//
//  YTFishpondHeaderView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/4.
//

#import "YTFishpondHeaderView.h"
@interface YTFishpondHeaderView ()
@property(nonatomic, strong)YTFishpondPMDview * pmdview;
@property(nonatomic, strong)UIImageView * snleftimg;
@property(nonatomic, strong)UIImageView * snrightimg;
@property(nonatomic, strong)UIImageView * swleftimg;
@property(nonatomic, strong)UIImageView * swrightimg;
@property(nonatomic, strong)UIButton * snBtn;
@property(nonatomic, strong)UIButton * swBtn;
@property(nonatomic, strong)UILabel * count;

@property(nonatomic, strong)UILabel * wszsCount;
@property(nonatomic, strong)UILabel * ycCount;
@property(nonatomic, strong)UILabel * jjCount;
@property(nonatomic, strong)UIView * backimg;
@property(nonatomic, strong)UIView * makeview;
@property(nonatomic, strong)UIView * buttomview;

@end
@implementation YTFishpondHeaderView

+ (instancetype)initview {
    YTFishpondHeaderView * view = [YTFishpondHeaderView new];
    view.clipsToBounds = YES;
    [view setupView];
    return view;
}
- (void)setupView{
    MJWeakSelf
    UIView * makeview = [UIView new];
    self.makeview = makeview;
    makeview.addClickBlock(^{
        [weakSelf openmessage];
    });
    makeview.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(10) andView:makeview];
    [self addSubview:makeview];
    [makeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(0);
        make.height.offset(kwt(75));
    }];
    
    UIImageView * tempimg = kimageView(@"alarm_message1");
    [makeview addSubview:tempimg];
    [tempimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(kwt(15));
        make.width.offset(kwt(20));
        make.height.offset(kwt(20));
    }];
    
    YTFishpondPMDview * pmdview = [YTFishpondPMDview initview];
    pmdview.userInteractionEnabled = NO;
    self.pmdview = pmdview;
    [makeview addSubview:pmdview];
    [pmdview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(40));
        make.top.offset(kwt(15));
        make.width.offset(kwt(300));
        make.height.offset(kwt(45));
    }];
    
    UIView * buttomview = [UIView new];
    [self addSubview:buttomview];
    self.buttomview = buttomview;
    [buttomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(85));
        make.height.offset(kwt(100));
    }];
    
    UIView * backview = [UIView new];
    backview.backgroundColor = KcolorHex(@"#F2F7FF");
    [buttomview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(kwt(30));
        make.height.offset(kwt(80));
    }];
    
  
    UIImageView * snrightimg = kimageView(@"unchecked_box_sw");
    self.snrightimg = snrightimg;
    [buttomview addSubview:snrightimg];
    [snrightimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.top.offset(kwt(4));
        make.width.offset(kwt(187));
        make.height.offset(kwt(27));
    }];    
    UIImageView * swleftimg = kimageView(@"unchecked_box_ws");
    self.swleftimg = swleftimg;
    [buttomview addSubview:swleftimg];
    [swleftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.offset(kwt(4));
        make.width.offset(kwt(187));
        make.height.offset(kwt(27));
    }];
    UIImageView * snleftimg = kimageView(@"checked_box_ws");
    self.snleftimg = snleftimg;
    [buttomview addSubview:snleftimg];
    [snleftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(0);
        make.width.offset(kwt(198));
        make.height.offset(kwt(31));
    }];
    UIImageView * swrightimg = kimageView(@"checked_box_sw");
    self.swrightimg = swrightimg;
    [buttomview addSubview:swrightimg];
    [swrightimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.offset(0);
        make.width.offset(kwt(198));
        make.height.offset(kwt(31));
    }];
    
    UIButton * snBtn= [UIButton buttonWithType:0];
    self.snBtn = snBtn;
    [snBtn addTarget:self action:@selector(SNBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [snBtn setTitle:@"温室" forState:UIControlStateNormal];
    [snBtn setTitleColor:KcolorHex(@"#0067E5") forState:UIControlStateNormal];
    snBtn.titleLabel.font = kCFont(17);
    [buttomview addSubview:snBtn];
    [snBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(swleftimg);
        make.width.offset(kwt(150));
        make.height.offset(kwt(27));
    }];
    
    UIButton * swBtn= [UIButton buttonWithType:0];
    self.swBtn = swBtn;
    [swBtn setTitle:@"外塘" forState:UIControlStateNormal];
    [swBtn addTarget:self action:@selector(SWBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [swBtn setTitleColor:KcolorHex(@"#0067E5") forState:UIControlStateNormal];
    swBtn.titleLabel.font = kCFont(17);
    [buttomview addSubview:swBtn];
    [swBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(snrightimg);
        make.width.offset(kwt(150));
        make.height.offset(kwt(27));
    }];
    
    UIView * btnview = [UIView new];
    btnview.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(7) andView:btnview];
    [backview addSubview:btnview];
    [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(kwt(12));
        make.right.offset(kwt(-12));
        make.height.offset(kwt(50));
    }];
    
    UIView * backimg = [UIView new];
    backimg.addClickBlock(^{
        [weakSelf backimgClick];
    });
    backimg.backgroundColor = KcolorHex(@"#F2F7FF");
    self.backimg = backimg;
    [DTKTool YJ:kwt(5) andView:backimg];
    [self addSubview:backimg];
    [backimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.bottom.equalTo(btnview);
        make.right.equalTo(btnview.mas_left);
    }];
    backimg.hidden = YES;
    UIImageView * leftbackimg = kimageView(@"left_black_path");
    [backimg addSubview:leftbackimg];
    [leftbackimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.width.height.offset(kwt(15));
    }];
    
    
    
    ///温室总数
    UIView * zsview = [UIView new];
    [btnview addSubview:zsview];
    [zsview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.offset(0);
        make.left.offset(0);
        make.width.offset(kwt(72));
    }];
    
    UILabel * wszsCount = [DTKTool labelWithText:@"0" andFont:kCFont(19) andTextColor:KcolorHex(@"#373737")];
    self.wszsCount = wszsCount;
    [zsview addSubview:wszsCount];
    [wszsCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(7));
        make.centerX.offset(0);
    }];
    
    UILabel * templab = [DTKTool labelWithText:@"温室总数" andFont:kFont(11) andTextColor:KcolorHex(@"#7D7D7D")];
    [zsview addSubview:templab];
    self.count = templab;
    [templab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(kwt(-7));
        make.centerX.offset(0);
    }];
    
    ///异常报警
    UIView * ycview = [UIView new];
    ycview.addClickBlock(^{
        if(weakSelf.openYCBlock){
            weakSelf.openYCBlock(1);
        }
    });
    [btnview addSubview:ycview];
    [ycview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.offset(0);
        make.left.equalTo(zsview.mas_right);
        make.width.offset(kwt(92));
    }];
    
    UILabel * ycCount = [DTKTool labelWithText:@"0" andFont:kCFont(19) andTextColor:KcolorHex(@"#F97519")];
    self.ycCount = ycCount;
    [ycview addSubview:ycCount];
    [ycCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(7));
        make.centerX.offset(0);
    }];
    
    UILabel * templab2 = [DTKTool labelWithText:@"异常告警" andFont:kFont(11) andTextColor:KcolorHex(@"#7D7D7D")];
    [ycview addSubview:templab2];
    [templab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(kwt(-7));
        make.centerX.offset(0);
    }];
    
    UIImageView * tempimg2 = kimageView(@"enter_details");
    [ycview addSubview:tempimg2];
    [tempimg2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(templab2.mas_right).offset(kwt(4));
        make.centerY.equalTo(templab2);
        make.width.height.offset(kwt(11));
    }];
    
    ///紧急
    UIView * jjview = [UIView new];
    jjview.addClickBlock(^{
        if(weakSelf.openYCBlock){
            weakSelf.openYCBlock(0);
        }
    });
    [btnview addSubview:jjview];
    [jjview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.offset(0);
        make.left.equalTo(ycview.mas_right);
        make.width.offset(kwt(92));
    }];
    
    UILabel * jjCount = [DTKTool labelWithText:@"0" andFont:kCFont(19) andTextColor:KcolorHex(@"#F31414")];
    self.jjCount = jjCount;
    [jjview addSubview:jjCount];
    [jjCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(7));
        make.centerX.offset(0);
    }];
    
    UILabel * templab3 = [DTKTool labelWithText:@"紧急告警" andFont:kFont(11) andTextColor:KcolorHex(@"#7D7D7D")];
    [jjview addSubview:templab3];
    [templab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(kwt(-7));
        make.centerX.offset(0);
    }];
    
    UIImageView * tempimg3 = kimageView(@"enter_details");
    [jjview addSubview:tempimg3];
    [tempimg3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(templab3.mas_right).offset(kwt(4));
        make.centerY.equalTo(templab3);
        make.width.height.offset(kwt(11));
    }];
    
    UIView * newTKview = [UIView new];
    newTKview.addClickBlock(^{
        [weakSelf XZTKClick];
    });
    newTKview.backgroundColor = KcolorHex(@"#ECF3FF");
    [DTKTool YJ:kwt(6) andView:newTKview];
    [btnview addSubview:newTKview];
    [newTKview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(3));
        make.right.bottom.offset(kwt(-3));
        make.width.offset(kwt(55));
    }];
    
    UIImageView * tkimg = kimageView(@"ic_fish_pedicure");
    [newTKview addSubview:tkimg];
    [tkimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(5));
        make.width.height.offset(kwt(18));
        make.centerX.offset(0);
    }];
    
    UILabel * templab4 = [DTKTool labelWithText:@"创建池塘" andFont:kCFont(10) andTextColor:KcolorHex(@"#2A79FC")];
    [newTKview addSubview:templab4];
    [templab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(28));
        make.centerX.offset(0);
    }];
    
}

- (void)XZTKClick{
    if(self.openXZTKBlock){
        self.openXZTKBlock();
    }
}
- (void)SNBtnClick{
    if(self.openSNBlock){
        self.openSNBlock();
    }
}


- (void)SWBtnClick{
    if(self.openSWBlock){
        self.openSWBlock();
    }
}
- (void)backimgClick{
    if(self.openSNBlock){
        self.openSNBlock();
    }
}
- (void)openmessage{
    
    if(self.openMessageBlock){
        self.openMessageBlock();
    }
}
-(void)setModel:(fishpondOutdoorModel *)model {
    _model = model;
    if(model.messageArray.count < 1){
        self.makeview.hidden = YES;
        [self.buttomview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
        }];
    } else {
        self.makeview.hidden = NO;
        self.pmdview.messageArray = model.messageListArray;
        [self.buttomview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.offset(kwt(85));
        }];
    }
    self.wszsCount.text = [NSString stringWithFormat:@"%lu", model.total];
    self.ycCount.text = [NSString stringWithFormat:@"%lu",model.abnormalAlarm];
    self.jjCount.text = [NSString stringWithFormat:@"%lu",model.criticalAlarm];
    self.backimg.hidden = YES;
    if(model.isWS == NO) {//室内
        self.swleftimg.hidden = YES;
        self.swrightimg.hidden = YES;
        self.swBtn.titleLabel.font = kFont(14);
        [self.swBtn setTitleColor:KcolorHex(@"#373737") forState:UIControlStateNormal];
        self.snleftimg.hidden = NO;
        self.snrightimg.hidden = NO;
        self.snBtn.titleLabel.font = kCFont(17);
        [self.snBtn setTitleColor:KcolorHex(@"#0067E5") forState:UIControlStateNormal];
        self.count.text = @"温室总数";
        [self.snBtn setTitle:@"温室" forState:UIControlStateNormal];
        if(model.name.length > 0){
            self.backimg.hidden = NO;
            [self.snBtn setTitle:[NSString stringWithFormat:@"温室-%@",model.name] forState:UIControlStateNormal];
            self.count.text = @"池塘总数";
        }
    } else {//室外
        self.swleftimg.hidden = NO;
        self.swrightimg.hidden = NO;
        self.swBtn.titleLabel.font = kCFont(17);
        [self.swBtn setTitleColor:KcolorHex(@"#0067E5") forState:UIControlStateNormal];
        self.snleftimg.hidden = YES;
        self.snrightimg.hidden = YES;
        self.snBtn.titleLabel.font = kFont(14);
        [self.snBtn setTitleColor:KcolorHex(@"#373737") forState:UIControlStateNormal];
        self.count.text = @"池塘总数";
    }
}

@end


@interface YTFishpondPMDview ()
@property (nonatomic ,strong) UILabel * name;
@property (nonatomic ,strong) UILabel * title;
@property (nonatomic ,strong) UILabel * createTime;
@property (nonatomic ,strong) UILabel * num;
@property (nonatomic ,strong) SYMessageModel * model;

@property (nonatomic ,strong) UILabel * name1;
@property (nonatomic ,strong) UILabel * title1;
@property (nonatomic ,strong) UILabel * createTime1;
@property (nonatomic ,strong) UILabel * num1;
@property (nonatomic ,assign) int i;

@property (nonatomic ,strong) NSTimer * timer;

@end
@implementation YTFishpondPMDview
+ (instancetype)initview{
    YTFishpondPMDview * view = [YTFishpondPMDview new];
    view.clipsToBounds = YES;
    UILabel * name = [DTKTool labelWithText:@"--" andFont:kCFont(15) andTextColor:KcolorHex(@"#27272A")];
    view.name = name;
    name.frame = CGRectMake(0, 0, kwt(150), kwt(20));
    [view addSubview:name];
    
    
    UILabel * num = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#ffffff")];
    view.num = num;
    num.backgroundColor = KcolorHex(@"#F93A4A");
    [DTKTool YJ:kwt(10) andView:num];
    num.textAlignment = NSTextAlignmentCenter;
    num.frame = CGRectMake(kwt(150 + 115), 0, kwt(35), kwt(20));
    [view addSubview:num];
    
    UILabel * createTime = [DTKTool labelWithText:@"--" andFont:kFont(14) andTextColor:KcolorHex(@"#979D9F")];
    view.createTime = createTime;
    createTime.textAlignment = NSTextAlignmentRight;
    createTime.frame = CGRectMake(kwt(150), 0, kwt(110), kwt(20));
    [view addSubview:createTime];
    
    UILabel * title = [DTKTool labelWithText:@"--" andFont:kFont(14) andTextColor:KcolorHex(@"#979D9F")];
    view.title = title;
    title.frame = CGRectMake(0, kwt(25), kwt(300), kwt(20));
    [view addSubview:title];
    
    UILabel * name1 = [DTKTool labelWithText:@"--" andFont:kCFont(15) andTextColor:KcolorHex(@"#27272A")];
    view.name1 = name1;
    name1.frame = CGRectMake(0, kwt(45), kwt(150), kwt(20));
    [view addSubview:name1];
    
    UILabel * num1 = [DTKTool labelWithText:@"--" andFont:kFont(15) andTextColor:KcolorHex(@"#ffffff")];
    view.num1 = num1;
    num1.backgroundColor = KcolorHex(@"#F93A4A");
    [DTKTool YJ:kwt(10) andView:num1];
    num1.textAlignment = NSTextAlignmentCenter;
    num1.frame = CGRectMake(kwt(150 + 115), kwt(45), kwt(35), kwt(20));
    [view addSubview:num1];
    
    UILabel * createTime1 = [DTKTool labelWithText:@"--" andFont:kFont(14) andTextColor:KcolorHex(@"#979D9F")];
    view.createTime1 = createTime1;
    createTime1.textAlignment = NSTextAlignmentRight;
    createTime1.frame = CGRectMake(kwt(150), kwt(45), kwt(110), kwt(20));
    [view addSubview:createTime1];
    
    UILabel * title1 = [DTKTool labelWithText:@"--" andFont:kFont(14) andTextColor:KcolorHex(@"#979D9F")];
    view.title1 = title1;
    title1.frame = CGRectMake(0, kwt(70), kwt(300), kwt(20));
    [view addSubview:title1];
    __weak typeof (view) weakview = view;
    UIView * clickview = [UIView new];
    clickview.addClickBlock(^{
        [weakview clickview];
    });
    clickview.frame = CGRectMake(0, 0, kwt(300), kwt(40));
    [view addSubview:clickview];
    return view;
}
- (void)clickview{
    //    if(self.openBlock && self.model){
    //        self.openBlock(self.model.name, self.model.type);
    //    }
}
- (void)setMessageArray:(NSArray<SYMessageModel *> *)messageArray{
    _messageArray = messageArray;
    [self removeTimer];
    self.name.text = messageArray.firstObject.title ;
    self.title.text = messageArray.firstObject.content;
    self.createTime.text = messageArray.firstObject.createTime;
    self.num.text = [NSString stringWithFormat:@"%d", messageArray.firstObject.num];
    if(messageArray.firstObject.num > 99){
        self.num.text = @"99+";
    }
    self.num.hidden = (messageArray.firstObject.num == 0);

    if(messageArray.count > 1){
        [self setupTimer];
    }
}

//加载定时器
- (void)setupTimer
{
    
    if (self.messageArray.count <= 1 ) {
        return;
    }
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerRun) userInfo:nil repeats:true];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

//移除定时器
- (void)removeTimer
{
    [_timer invalidate];
    _timer = nil;
}

- (void)timerRun
{
    if (!self.messageArray.count) {
        return;
    }
    SYMessageModel * NewModel ;
    if(self.i >= self.messageArray.count - 1){
        self.i = 0;
        NewModel = self.messageArray.firstObject;
    } else {
        NewModel = self.messageArray[self.i + 1];
        self.i += 1;
    }
    self.model = NewModel;
    self.name1.text = NewModel.title;
    self.title1.text = NewModel.content ;
    self.createTime1.text = NewModel.createTime;
    self.num1.text = [NSString stringWithFormat:@"%d", NewModel.num];
    if(NewModel.num > 99){
        self.num1.text = @"99+";
    }
    self.num1.hidden = (NewModel.num == 0);
    
    
    [UIView animateWithDuration:0.5 animations:^{
        self.name.frame = CGRectMake(0, kwt(-45), kwt(150), kwt(20));
        if(self.createTime.mj_w == kwt(110)){
            self.createTime.frame = CGRectMake(kwt(150), kwt(-45), kwt(110), kwt(20));
        } else {
            self.createTime.frame = CGRectMake(kwt(150), kwt(-45), kwt(150), kwt(20));
        }
        self.title.frame = CGRectMake(0, kwt(-20), kwt(300), kwt(20));
        self.num.frame = CGRectMake(kwt(150 + 115), kwt(-45), kwt(35), kwt(20));
        
        self.name1.frame = CGRectMake(0, 0, kwt(150), kwt(20));
        self.title1.frame = CGRectMake(0, kwt(25), kwt(300), kwt(20));
        if(NewModel.num == 0){
            self.createTime1.frame = CGRectMake(kwt(150), 0, kwt(150), kwt(20));
        } else {
            self.createTime1.frame = CGRectMake(kwt(150), 0, kwt(110), kwt(20));
        }
        self.num1.frame = CGRectMake(kwt(150 + 115), 0, kwt(35), kwt(20));

    } completion:^(BOOL finished) {
        self.name.text = NewModel.title;
        self.createTime.text = NewModel.createTime;
        self.title.text = NewModel.content ;
        self.num.text = [NSString stringWithFormat:@"%d", NewModel.num];
        if(NewModel.num > 99){
            self.num.text = @"99+";
        }
        self.num.hidden = (NewModel.num == 0);

        self.name.frame = CGRectMake(0, 0, kwt(150), kwt(20));
        self.createTime.frame = CGRectMake(kwt(150), 0, kwt(110), kwt(20));
        self.title.frame = CGRectMake(0, kwt(25), kwt(300), kwt(20));
        self.num.frame = CGRectMake(kwt(150 + 115), 0, kwt(35), kwt(20));
        if(NewModel.num == 0){
            self.createTime.frame = CGRectMake(kwt(150), 0, kwt(150), kwt(20));
        } else {
            self.createTime.frame = CGRectMake(kwt(150), 0, kwt(110), kwt(20));
        }
        
        self.name1.frame = CGRectMake(0, kwt(45), kwt(150), kwt(20));
        self.createTime1.frame = CGRectMake(kwt(150), kwt(45), kwt(110), kwt(20));
        self.title1.frame = CGRectMake(0, kwt(70), kwt(300), kwt(20));
        self.num1.frame = CGRectMake(kwt(150 + 115), kwt(45), kwt(35), kwt(20));
        
    }];
    
}
@end
